var tpl = require('../../views/list');
var datagrid = require('{com}/datagrid');
var util = require('util');
var request = require('request');
var load = require('{pub}/load');
var layer = require('{pub}/layer');
var template = require('template');
var router = require('router');
var func = require('{pub}/function');

return {
    path:'/list/',
    entry:true,
    data:{
        title:'账套管理',
        conid:'listbox',
        company:{}
    },
    template:tpl,
    events:{
        //新增账套
        'click .j-add':'edit',
        //分配权限
        'click .j-assign':'assign',
        //回收站
        'click .j-recycle':'recycle',
        //进入账套
        'dblclick .table-row':'getData _entry',
        'click .j-entry':'getData _entry',
        //修改账套
        'click .j-update':'getData edit',
        //删除账套
        'click .j-del':'getData del',
        //查看企业列表
        'click .j-company':require('./company'),
        //企业信息维护
        'click .j-manage':require('./manage'),
        //点击用户名
        'click .j-user':require('./showuser')
    },
    onInit:function(){
        var that = this;

        //检测是否初次建账
        /*request.get('account/getCountInCompany', {}, {
            '200':function(res){
                if(res.data.count === 0){
                    router.location('create')
                }
            }
        }, null)*/ 

        //获取企业列表
        request.get('company/findAll', {}, {
            '200':function(res){
                that.loadCompany(res.data.companyList);
            }
        }, '正在初始化企业信息...')

        //渲染用户信息
        load.user(function(res){
            $('#username').html(res.data.user.realName)
        })
    },
    loadCompany:function(list){
        var that = this;
        //是否择默认企业
        var isDefault = false;
        //当前企业
        var company = null;
        //企业列表
        that.companyList = list||[];

        Nui.each(list, function(v, k){
            if(!isDefault && v.isDefault){
                isDefault = true;
            }
            if(!company && v.isCurrent){
                company = v;
            }
            if(isDefault && company){
                return false
            }
        })
        
        //为选择默认企业
        if(list.length > 1 && !isDefault){
            that.chooseCompany();
        }

        //渲染列表
        that.renderList();
        //获取当前企业数据
        that.renderCompany(company);
    },
    renderCompany:function(data){
        //选择企业
        if(data && typeof data !== 'object'){
            Nui.each(this.companyList, function(v){
                if(v.id === data){
                    data = v;
                    return false
                }
            })
        }
        
        var company = this.data.company = data;
        //企业是否过期
        if(company.useStatus > 1){
            this.chooseCompany();
        }
        $('#companyname').html(company.name||'');

        if(company.id){
            this.paging.condition.companyId = company.id;
            this.paging.query(true)
        }
        
    },
    //帐套列表
    renderList:function(){
        var that = this;
        that.datagrid = datagrid({
            container:'#listbox',
            isBorder:false,
            fields:['id', 'name'],
            isPaging:false,
            dataField:'data.accountList',
            placeholder:'暂无帐套',
            paging:{
                url:'account/getList',
                isPage:false
            },
            columns:[{
                title:'编号',
                content:'number',
                width:'90'
            }, {
                title:'账套名称',
                field:'name',
                width:'15%',
                align:'left',
                className:'f-tal',
                nowrap:true
            }, {
                title:'建账日期',
                field:'createDate',
                width:'100',
                align:'left',
                className:'f-tal',
                nowrap:true
            }, {
                title:'记账状态',
                field:'accountStatus',
                width:'10%',
                align:'left',
                className:'f-tal',
                nowrap:true
            }, {
                title:'凭证总数',
                field:'voucherCount',
                width:'10%',
                align:'left',
                className:'f-tal',
                nowrap:true
            }, {
                title:'财务主管',
                field:'buname',
                width:'10%',
                align:'left',
                className:'f-tal',
                nowrap:true
            }, {
                title:'记账会计',
                field:'buname',
                width:'10%',
                align:'left',
                className:'f-tal',
                nowrap:true
            }, {
                title:'出纳',
                field:'buname',
                width:'10%',
                align:'left',
                className:'f-tal',
                nowrap:true
            }, {
                title:'操作',
                align:'left',
                className:'f-tal',
                filter:function(){
                    return renders({
                        <button class="ui-button ui-button-blue ui-button-line ui-button-flat e-mr20 j-entry">进入账套</button>
                        <a class="f-cblue e-mr20 f-wsn j-update f-vam"><i class="iconfont e-mr5">&#xe63b;</i>修改</a>
                        <span class="f-dib f-vam j-del"><a class="f-cblue f-wsn"><i class="iconfont e-mr5">&#xe63d;</i>删除</a></span>
                    })
                }
            }],
            onRender:function(self){
                //企业到期
                if(that.data.company.useStatus > 1){
                    that.element.find('.ui-button').addClass('ui-button-disabled')
                }
            }
        });

        that.paging = that.datagrid.paging;
    },
    getData:require('{pub}/function').getData,
    edit:require('./edit'),
    assign:require('./assign'),
    recycle:require('./recycle'),
    _entry:function(e, elem, data){
        //require('./choose')(data, '正在进入账套...')
    },
    del:require('./delete'),
    //选择企业
    chooseCompany:require('./chooseCompany')
}